package pl.edu.agh.uddiProxy.client.admin;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class UserDetailsGrid extends Grid {
	private TextBox usernameTextBox = new TextBox();
	private TextBox firstnameTextBox = new TextBox();
	private TextBox lastnameTextBox = new TextBox();
	private TextBox emailTextBox = new TextBox();
	private PasswordTextBox passwordTextBox = new PasswordTextBox();
	private PasswordTextBox password2TextBox = new PasswordTextBox();
	private Button saveButton; 
	
	public UserDetailsGrid() {
		super(7, 2);
		int row = 0;
		
		setText(row, 0, "Username");
		setWidget(row++, 1, usernameTextBox);
		setText(row, 0, "Firstname");
		setWidget(row++, 1, firstnameTextBox);
		setText(row, 0, "Lastname");
		setWidget(row++, 1, lastnameTextBox);
		setText(row, 0, "Email");
		setWidget(row++, 1, emailTextBox);
		setText(row, 0, "Password");
		setWidget(row++, 1, passwordTextBox);
		setText(row, 0, "Re-enter password");
		setWidget(row++, 1, password2TextBox);
		
		saveButton = new Button("Save", new ClickListener() {
		
			public void onClick(Widget arg0) {
				saveUser();
			}
		});
		setWidget(row++, 1, saveButton);
	}
	
	private User getUser() {
		String username = usernameTextBox.getText();
		String firstname = firstnameTextBox.getText();
		String lastname = lastnameTextBox.getText();
		String email = emailTextBox.getText();
		String password = passwordTextBox.getText();
		String password2 = password2TextBox.getText();
		
		if (!password.equals(password2)) {
			Window.alert("Passwords do not match");
			return null;
		}
		if (username == null || username.trim().length() == 0 ||
				firstname == null || firstname.trim().length() == 0 ||
				lastname == null || lastname.trim().length() == 0 ||
				email == null || email.trim().length() == 0 ||
				password == null || password.trim().length() == 0) {
			Window.alert("Fill all fields");
			return null;
		}
		
		return new User(username, firstname, lastname, password, email);
	}
	
	public void setUser(User user) {
		usernameTextBox.setText(user.getUsername());
		usernameTextBox.setEnabled(false);
		firstnameTextBox.setText(user.getFirstName());
		lastnameTextBox.setText(user.getLastName());
		emailTextBox.setText(user.getEmail());
	}
	
	public void clear() {
		usernameTextBox.setText("");
		usernameTextBox.setEnabled(true);
		firstnameTextBox.setText("");
		lastnameTextBox.setText("");
		emailTextBox.setText("");
		passwordTextBox.setText("");
		password2TextBox.setText("");
	}

	private void saveUser() {
		User user = getUser();
		UserServiceAsync serviceAsync = UserService.Util.getInstance();
		AsyncCallback<Void> callback = new AsyncCallback<Void>() {
		
			public void onSuccess(Void v) {
				clear();
				update();
			}
		
			public void onFailure(Throwable throwable) {
				if (throwable instanceof UserAlreadyExists) {
					Window.alert("User already exists");
				} else {
					Window.alert(throwable.getMessage());
				}
			}
		};
		
		if (user != null) {
			if (usernameTextBox.isEnabled()) {
				serviceAsync.createUser(user, callback);
			} else {
				serviceAsync.updateUser(user, callback);
			}
		}
	}
	
	protected void update() {
	}
}
